package com.da.lon.wang.xlg.view;

import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;

public class MyWebImage extends ImageView {

    private OnClickListener onClickListener;
    private ImageLoader imageLoader;

    public MyWebImage(Context context) {
        super(context);
        initDate();
    }

    public MyWebImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDate();
    }

    public MyWebImage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initDate();
    }

    public void setMyWebListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setUrlAsync(String url) {
        imageLoader.displayImage(url, this);
    }

    private void initDate() {
        imageLoader = ImageLoader.getInstance();
    }

    private void changeLight(int brightness) {
        ColorMatrix cMatrix = new ColorMatrix();
        cMatrix.set(new float[]{1, 0, 0, 0, brightness, 0, 1, 0, 0,
                brightness,// 改变亮度
                0, 0, 1, 0, brightness, 0, 0, 0, 1, 0});
        setColorFilter(new ColorMatrixColorFilter(cMatrix));
    }

    /*
     * (non-Javadoc)
     *
     * @see android.view.View#onTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                changeLight(0);
                if (null != onClickListener) {
                    onClickListener.onClick(MyWebImage.this);
                }
                break;
            case MotionEvent.ACTION_DOWN:
                changeLight(-50);
                break;
            case MotionEvent.ACTION_CANCEL:
                changeLight(0);
                break;
            default:
                break;
        }
        return true;
    }
}
